package morenoapp.components.help;

import java.awt.Desktop;
import java.awt.Dimension;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;
import java.io.IOException;
import java.net.URI;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JButton;
import javax.swing.JComponent;
import javax.swing.JToolBar;
import morenoapp.MorenoApp;
import morenoapp.logging.MorenoLog;
import morenoapp.resources.ResourcesHolder;
import morenoapp.util.I18N;
import morenoapp.util.MorenoUtil;

/**
 *
 * @author root
 */
public enum MorenoHelp {
    Intro(null),
    About(null),
    Developers(null),
    Sociometric(null),
    SociometricsIntro(Sociometric),
    SociometricsPoll(Sociometric);

    
    MorenoHelp parentNode;
    private MorenoHelp(MorenoHelp parentNode) {
        this.parentNode = parentNode;
    }


    public static JButton createHelpButton(final String name) {
        final JButton button = new JButton(ResourcesHolder.iconHelpSmall);
        button.setName(name);
        button.setPreferredSize(new Dimension(24, 24));
        button.setToolTipText(I18N.get("tooltip.show_help"));
        button.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                if (MorenoUtil.isDebug()) {
                    MorenoUtil.logKeyToFile(new File("missed_help_topics.txt"), name);
                }
                URI uri = URI.create("file:///" + new File("doc/files/").getAbsolutePath().replace("\\", "/") + "/" + name + ".html");
                try {
                    Desktop.getDesktop().browse(uri);
                } catch (IOException ex) {
                    MorenoLog.notifyException(I18N.get("error.cant_open_file")+" "+uri, ex);
                }
            }
        });
        return button;
    }

    public static JButton createHelpButton(final JComponent context) {
        return createHelpButton(context.getClass().getSimpleName());

    }

    public static JComponent createHelpToolbar(final String name) {
        JToolBar toolbar = new JToolBar(JToolBar.HORIZONTAL);
        toolbar.setFloatable(false);
        toolbar.setBorderPainted(false);
        toolbar.add(createHelpButton(name));
        return toolbar;
    }

    public static JComponent createHelpToolbar(JComponent context) {
        return createHelpToolbar(context.getClass().getSimpleName());
    }
}
